package http_client

import (
	"context"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/parnurzeal/gorequest"
)

const (
	OPEN_SEARCH_HOST  = "open_search_host"
)

type OpenSearchHttpClient struct {
	client *HttpClientTool
}

// NewOpenSearchHttpClient
// 供方SaaS搜索服务http client工具类
func NewOpenSearchHttpClient() *OpenSearchHttpClient {
	return &OpenSearchHttpClient{
		client: New(),
	}
}

// Get
// 搜索服务Get请求
func (c *OpenSearchHttpClient) Get(ctx context.Context, path string, params map[string]interface{}) *OpenSearchHttpClient {
	url := stark.Configer.(stark.ApolloConfigerIface).GetStringValue(OPEN_SEARCH_HOST, "") + path
	url = JoinQueryStr(url, params)

	c.client = c.client.Get(ctx, url, nil)
	return c
}

// Post
// 搜索服务Post请求
func (c *OpenSearchHttpClient) Post(ctx context.Context, path string, params map[string]interface{},header map[string]string) *OpenSearchHttpClient {
	url := stark.Configer.(stark.ApolloConfigerIface).GetStringValue(OPEN_SEARCH_HOST, "") + path

	c.client = c.client.Post(ctx, url, params,header)
	return c
}

// EndStruct
// convert interface(v) to struct
func (c *OpenSearchHttpClient) EndStruct(v interface{}) (gorequest.Response, []byte, []error) {
	return c.client.EndStruct(v)
}

//分页数据
type OpenSearchResponse struct {
	Success   bool   `json:"success"`
	Message   string `json:"message"`
	ErrorCode int    `json:"errorCode"`
	Data      struct {
		Total int                      `json:"total"`
		List  []map[string]interface{} `json:"list"`
	} `json:"data"`
}
